<?php
class Kal_Banners_Block_Post extends Mage_Core_Block_Template
{
    public function getPost()
   	{
        if (!$this->hasData('post')) {
            if ($this->getPostId()) {
                $post = Mage::getModel('banners/post')
					->setStoreId(Mage::app()->getStore()->getId())
                    ->load($this->getPostId(), 'post_id');
            } else {
                $post = Mage::getSingleton('banners/post');
            }
			$cat = Mage::getSingleton('banners/cat')->load($this->getRequest()->getParam('cat'), "identifier");
			$route = Mage::getStoreConfig('banners/banners/route');
			if ($route == "")
			{
				$route = "banners";
			}
			$route = Mage::getUrl($route);
			if ($cat->getIdentifier() != null)
			{
				$post->setAddress($route . 'cat/' . $cat->getIdentifier() ."/post/" . $post->getIdentifier());
			}
			else
			{
				$post->setAddress($route . $post->getIdentifier());
			}
			$post->setCreatedTime($this->formatTime($post->getCreatedTime(),Mage::getStoreConfig('banners/banners/dateformat'), true));
			$post->setUpdateTime($this->formatTime($post->getUpdateTime(),Mage::getStoreConfig('banners/banners/dateformat'), true));
			
			$cats = Mage::getModel('banners/cat')->getCollection()
			->addPostFilter($post->getPostId());
			$catUrls = array();
			foreach($cats as $cat)
			{
				$catUrls[$cat->getTitle()] = $route . "cat/" . $cat->getIdentifier();
			}
			$post->setCats($catUrls);
			
            $this->setData('post', $post);
        }
        return $this->getData('post');
    }
	
	public function getBookmarkHtml($post)
	{
		if (Mage::getStoreConfig('banners/banners/bookmarkspost'))
		{
			$this->setTemplate('banners/bookmark.phtml');
			$this->setPost($post);
			return $this->toHtml();
		}
		return;
	}
	
	public function getComment()
   	{
		$post = $this->getPost();
		
        $collection = Mage::getModel('banners/comment')->getCollection()
		->addPostFilter($post->getPostId())
		->setOrder('created_time ', 'asc')
		->addApproveFilter(2); 

		return $collection;

    }
	
	public function getCommentTotalString($comments)
	{
		$comment_count = count($comments);
		if ($comment_count == 1) {
			$comment_string = $comment_count . " " . Mage::helper('banners')->__('Comment');
		}
		else {
			$comment_string = $comment_count . " " . Mage::helper('banners')->__('Comments');
		}
		return $comment_string;
	}
	
	public function getCommentsEnabled()
   	{
		return Mage::getStoreConfig('banners/comments/enabled');
	}
	
	public function getLoginRequired()
   	{
		return Mage::getStoreConfig('banners/comments/login');
	}
	
	public function getFormAction()
    {
        return $this->getUrl('*/*/post');
    }
	
	protected function _prepareLayout()
    {
        $post = $this->getPost();
		$cat = Mage::getSingleton('banners/cat')->load($this->getRequest()->getParam('cat'), "identifier");
		$route = Mage::getStoreConfig('banners/banners/route');
		if ($route == "")
		{
			$route = "banners";
		}
        // show breadcrumbs
        if (Mage::getStoreConfig('banners/banners/bannerscrumbs') && ($breadcrumbs = $this->getLayout()->getBlock('breadcrumbs'))){
			$breadcrumbs->addCrumb('home', array('label'=>Mage::helper('banners')->__('Home'), 'title'=>Mage::helper('banners')->__('Go to Home Page'), 'link'=>Mage::getBaseUrl()));;
			$breadcrumbs->addCrumb('banners', array('label'=>Mage::getStoreConfig('banners/banners/title'), 'title'=>Mage::helper('banners')->__('Return to ' .Mage::getStoreConfig('banners/banners/title')), 'link'=>Mage::getUrl($route)));
			if ($cat->getTitle() != "")
			{
				$breadcrumbs->addCrumb('cat', array('label'=>$cat->getTitle(), 'title'=>Mage::helper('banners')->__('Return to ' .$cat->getTitle()), 'link'=>Mage::getUrl($route . '/cat') . $cat->getIdentifier()));
			}
			$breadcrumbs->addCrumb('banners_page', array('label'=>$post->getTitle(), 'title'=>$post->getTitle()));
        }

		if ($head = $this->getLayout()->getBlock('head')) {
            $head->setTitle($post->getTitle());
            $head->setKeywords($post->getMetaKeywords());
            $head->setDescription($post->getMetaDescription());
        }
    }
    
	public function doFormatMoney($money)
	{
	    $tmp_money = strrev($money);
	    $format_money = ""; 
	    for($i = 3;$i<strlen($money);$i+=3){
	        $format_money .= substr($tmp_money,0,3).",";
	         $tmp_money = substr($tmp_money,3);
	     }
	    $format_money .=$tmp_money;
	    $format_money = strrev($format_money); 
	    return $format_money;
	}

}